<?php
//停用插件
function webp_uninstall() {
    delete_option('webp_quality');
    delete_option('webp_width');
    delete_option('webp_height');
}

//文件类型判断
function webp_is_image($type) {
    $mime = [
        'image/jpg',
        'image/jpeg',
        'image/png',
        //'image/gif'
    ];
    return in_array($type, $mime);
}

//图片缩放大小
function webp_image_size($image) {
    $maxwidth = get_option('webp_width', 0);
    $maxheight = get_option('webp_height', 0);

    if(!$maxwidth && !$maxheight) {
        return false;
    }

    $imagesize = @getimagesize($image);
    $width = $imagesize[0];
    $height = $imagesize[1];
    if($width <= $maxwidth && $height <= $maxheight) {
        return false;
    }

    if($maxwidth > 0 && $width > $maxwidth) {
        $ratio = $maxwidth/$width;
        $width = $maxwidth;
        $height *= $ratio;
    }

    if($maxheight > 0 && $height > $maxheight) {
        $ratio = $maxheight/$height;
        $height = $maxheight;
        $width *= $ratio;
    }

    return [
        'o_w' => $imagesize[0],
        'o_h' => $imagesize[1],
        'w' => intval($width),
        'h' => intval($height)
    ];
}

//GD
function webp_convert_image_gd($file, $new_file, $type) {
    $quality = get_option('webp_quality', 70);
    $methods = array(
        'image/jpg' => 'imagecreatefromjpeg',
        'image/jpeg' => 'imagecreatefromjpeg',
        'image/png' => 'imagecreatefrompng',
        'image/gif' => 'imagecreatefromgif'
    );

    $image = $methods[$type]($file);
    $size = webp_image_size($file);
    if($size) {
        $image_p = imagecreatetruecolor($size['w'], $size['h']);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $size['w'], $size['h'], $size['o_w'], $size['o_h']);
        imagewebp($image_p, $new_file, $quality);
        imagedestroy($image_p);
    } else {
        imagewebp($image, $new_file, $quality);
    }

    imagedestroy($image);
}

//Imagick
function webp_convert_image_imagick($file, $new_file, $type) {
    $image = new Imagick($file);
    $quality = get_option('webp_quality', 70);
    $size = webp_image_size($file);

    if($size) {
        $image->thumbnailImage($size['w'], $size['h']);
    }
    
    $image->setImageFormat('webp');
    $image->stripImage();
    $image->setImageCompressionQuality($quality);
    $image->writeImage($new_file);
    $image->destroy();
}

//文件名过滤
function webp_file_name($filename) {
    $pathinfo = pathinfo($filename);
    $type = isset($pathinfo['extension']) ? 'image/' . $pathinfo['extension'] : '';
    if($type && webp_is_image($type)) {
        $filename = $pathinfo['filename'] . '.webp';
    }
    return $filename;
}
add_filter('sanitize_file_name', 'webp_file_name', 10, 1);

//上传文件处理
function webp_upload_file($return, $file, $new_file, $type) {
    if(!webp_is_image($type)) return null;

    if(extension_loaded('imagick') && !empty(\Imagick::queryformats('webp'))) {
        webp_convert_image_imagick($file['tmp_name'], $new_file, $type);
    } elseif(extension_loaded('gd') && function_exists('imagewebp')) {
        webp_convert_image_gd($file['tmp_name'], $new_file, $type);
    }

    $stat  = stat(dirname($new_file));
    $perms = $stat['mode'] & 0000666;
    chmod($new_file, $perms);
    unlink($file['tmp_name']);
    return true;
}
add_action('pre_move_uploaded_file', 'webp_upload_file', 10, 4);

//修改上传信息
function webp_handle_upload($file) {
	if(!webp_is_image($file['type'])) return $file;

    $file['type'] = 'image/webp';
	return $file;
}
add_action('wp_handle_upload', 'webp_handle_upload', 10, 1);
?>